/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#ifndef SX_OBJ_FILE_DEFINITION_H
#define SX_OBJ_FILE_DEFINITION_H

#include <sxBufferUtilities.h>
#include <sxMath.h>
#include <sxMap.h>
#include <sxOBJ_Mesh.h>
#include <sxString.h>

namespace Sx {
namespace Graphics {
namespace File {

/*
 * Mesh name to be used when a loaded OBJ file does not provide a name.
 */
const static String OBJ_NO_MESH_NAME = "NoName";
const static String OBJ_NO_MATERIAL = "NoMaterial";
const static String OBJ_NO_GROUP = "NoGroup";

/*
 * This structure contains the definition of an OBJ file. All possible
 * values and structures that can define an OBJ files contents should
 * be stored in this structure.
 */
struct OBJ_FileDefinition {

	~OBJ_FileDefinition() {
		this->clear();
	}

	/*
	 * Instead of creating a new file definition for an OBJ file definition
	 * alreadly allocated, this function allows this definition to be reused.
	 * The contents of each map, array used in this definition are cleared.
	 */
	void clear() {
		for ( unsigned int i = 0; i < this->meshes.size(); i++ )
			delete this->meshes[i];
		this->meshes.clear();
		this->comments.clear();
		this->groups.clear();
		this->materialLibraries.clear();
	 	this->materials.clear();
	}

	/*
	 * Returns the last mesh from the set of meshes.
	 */
	OBJ_Mesh* getLastMesh() {
		if ( this->meshes.size() == 0 ) return nullptr;
		return this->meshes[this->meshes.size() - 1];
	}

	/*
	 * Adds a new mesh with no name provided.
	 */
	void addMesh() {
		this->meshes.add(new OBJ_Mesh(OBJ_NO_MESH_NAME));
	}

	void addMesh(OBJ_Mesh* mesh) {
		this->meshes.add(mesh);
	}

	/*
	 * Adds a new mesh to this object definition with the specified name.
	 */
	void addMesh(const String& name) {
		this->meshes.add(new OBJ_Mesh(name));
	}

	/*
	* An OBJ file can contain more than one mesh. Each of the
	* defined meshes are stored individually as they are seen
	* in a loaded OBJ file.
	*/
	Sx::Util::ArrayList<OBJ_Mesh*> meshes;

	/*
	* Stores all of the comments in the OBJ file.
	*/
	Sx::Util::ArrayList<String> comments;

	/*
	* Stores all of the group names and the indices associated with
	* those groups.
	*/
	Sx::Util::Map<OBJ_GroupIndex, String> groups;

	/*
	 * Stores all of the external material libraries that are referenced
	 * from this OBJ definition.
	 */
	Sx::Util::ArrayList<String> materialLibraries;

	/*
	* Stores the names of all materials and their associated indices
	* that are stored in each face. Each faces material name can be
	* determined by looking at its material index and using that as
	* a lookup index in this map. The string representation of that
	* material can be returned.
	*/
	Sx::Util::Map<OBJ_MaterialIndex, String> materials;
};

}

}

}

#endif
